package com.aco.fatureapp.printer.libs;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;

import com.aco.fatureapp.R;
import com.aco.fatureapp.model.printer.Billing;
import com.aco.fatureapp.utilitary.CallAsyncTask;

public class PrinterTesteActivity extends Activity {

	Printer2 mPrinter = new Printer2();

	DrawView mDrawing;
	Bitmap mBitmap = null;
	Integer mDensity = 10;

	Billing mBilling;

	public static String CONTA = "PrinterTesteActivity.conta";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Pegar o bitmap como parâmetro.
		Intent intent = getIntent();
		mBilling = (Billing) intent.getSerializableExtra(CONTA);

		if (mBilling != null) {
			mBitmap = Printer2.gerarPrimeiraParte(mBilling);
		}

		mDrawing = new DrawView(this);
		ScrollView.LayoutParams lp = new ScrollView.LayoutParams(1000, 1000);
		setContentView(mDrawing, lp);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_imprimir: {

			CallAsyncTask callAsyncTask = new CallAsyncTask(
					PrinterTesteActivity.this, true);

			callAsyncTask.execute(new CallAsyncTask.Action() {

				@Override
				public String execute() {

					// Bitmap mBitmap =
					// mPrinter.gerar(PrinterTesteActivity.this,
					// mBilling, false);
					//
					// if (mBitmap != null) {
					//
					// if (mPrinter.checkBth(PrinterTesteActivity.this)) {
					//
					// if (mBitmap != null) {
					// ESCP.ImageToEsc(mBitmap, mPrinter.mBth.Ostream,
					// 8, 10);
					// }
					// }
					// mPrinter.closeBth();
					// }

					return null;
				}
			});

			return true;
		}
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private class DrawView extends View {
		private boolean move = false;
		private int X = 0, Y = 0, iX = 0, iY = 0;

		public DrawView(Context context) {
			super(context);
			this.setBackgroundResource(R.color.window_background);
		}

		@Override
		public boolean onTouchEvent(final MotionEvent event) {
			boolean handled = false;
			int xTouch;
			int yTouch;
			int pointerId;
			int actionIndex = event.getActionIndex();

			switch (event.getActionMasked()) {
			case MotionEvent.ACTION_DOWN: {
				xTouch = (int) event.getX(0);
				yTouch = (int) event.getY(0);

				iX = (xTouch - X);
				iY = (yTouch - Y);

				invalidate();
				handled = true;
				move = true;
				break;
			}
			case MotionEvent.ACTION_POINTER_DOWN: {
				pointerId = event.getPointerId(actionIndex);

				xTouch = (int) event.getX(actionIndex);
				yTouch = (int) event.getY(actionIndex);

				iX = (xTouch - X);
				iY = (yTouch - Y);

				invalidate();
				handled = true;
				move = true;
				break;
			}
			case MotionEvent.ACTION_MOVE: {
				final int pointerCount = event.getPointerCount();

				for (actionIndex = 0; actionIndex < pointerCount; actionIndex++) {
					pointerId = event.getPointerId(actionIndex);

					xTouch = (int) event.getX(actionIndex);
					yTouch = (int) event.getY(actionIndex);

					if (move) {
						X = (xTouch - iX);
						Y = (yTouch - iY);
					}
				}
				invalidate();
				handled = true;
				break;
			}
			case MotionEvent.ACTION_UP: {
				move = false;
				invalidate();
				handled = true;
				break;
			}
			case MotionEvent.ACTION_POINTER_UP: {
				move = false;
				pointerId = event.getPointerId(actionIndex);
				invalidate();
				handled = true;
				break;
			}
			case MotionEvent.ACTION_CANCEL: {
				move = false;
				handled = true;
				break;
			}
			}
			return super.onTouchEvent(event) || handled;
		}

		@Override
		protected void onDraw(Canvas canvas) {
			if (mBitmap != null) {
				Paint myPaint = new Paint();
				myPaint.setColor(Color.BLACK);

				boolean resize = false;
				if (!resize) {
					canvas.drawBitmap(mBitmap, X, Y, myPaint);
				}
				else {
					int ih = mBitmap.getHeight();
					int iw = mBitmap.getWidth();
					int mh = getHeight();
					float fat = (ih / mh);
					int mw = (iw * mh) / ih;
					canvas.drawBitmap(mBitmap, new Rect(0, 0, iw, ih),
							new Rect(0, 0, mw, mh), myPaint);
				}
			}

		}
	}

}
